package com.huangpuweb.task.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huangpuweb.task.Constants;
import com.huangpuweb.task.model.HpScenicNumber;
import com.huangpuweb.task.model.HpScenicRealTime;
import com.huangpuweb.task.service.SyncScenicSpotService;
import com.huangpuweb.task.util.DateUtil;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author rufeng
 * @date 2021/2/17 15:47
 */
@Controller
public class HpScenicSpotJob {
    @Resource
    private SyncScenicSpotService syncScenicSpotService;

    public void  init(){
        loginYuYuan();
        loginxintiandi();
        logintianzifang();
        loginwaitan();
    }
    /**
     * 新接口对接，先入库再上屏. 用户登录返回用户UID和景点阈值
     */
    public void loginYuYuan() {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            //http://121.40.116.34:8077,正式环境http://11.66.236.1:8080/
            HttpPost post = new HttpPost("http://11.66.236.1:8080/User/Login");
            post.setHeader("Content-Type", "application/json;charset=utf-8");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("usercode", Constants.YUYUAN_USERCODE);
            jsonObject.put("password", Constants.YUYUAN_PASSWORD);
            StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
            post.setEntity(s);
            HttpResponse execute = httpClient.execute(post);
            String entity = EntityUtils.toString(execute.getEntity(), "utf-8");
            JSONObject jsonObject1 = JSON.parseObject(entity);
            LogFactory.getLog(this.getClass()).info("获取接口数据成功loginYuYuan：http://121.40.116.34:8077/User/Login");
            if (jsonObject1.getString("success") == "true") {
                JSONObject data = JSON.parseObject(jsonObject1.getString("data"));
                //将登陆获取对应账号的uuid写入缓存中
                Constants.map.put("yuyuan_uuid", data.getString("uuid"));
                insertCommons(data);

            }
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).error("获取接口数据失败loginYuYuan：http://121.40.116.34:8077/User/Login");
        }
    }

    public void loginxintiandi() {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://11.66.236.1:8080/User/Login");
            post.setHeader("Content-Type", "application/json;charset=utf-8");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("usercode", Constants.XINTIANDI_USERCODE);
            jsonObject.put("password", Constants.XINTIANDI_PASSWORD);
            StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
            post.setEntity(s);
            HttpResponse execute = httpClient.execute(post);
            String entity = EntityUtils.toString(execute.getEntity(), "utf-8");
            JSONObject jsonObject1 = JSON.parseObject(entity);
            LogFactory.getLog(this.getClass()).info("获取接口数据成功loginxintiandi：http://121.40.116.34:8077/User/Login");
            if (jsonObject1.getString("success") == "true") {
                JSONObject data = JSON.parseObject(jsonObject1.getString("data"));
                Constants.map.put("xintiandi_uuid", data.getString("uuid"));
                insertCommons(data);

            }
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).error("获取接口数据失败loginxintiandi：http://121.40.116.34:8077/User/Login");
        }
    }

    public void logintianzifang() {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://11.66.236.1:8080/User/Login");
            post.setHeader("Content-Type", "application/json;charset=utf-8");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("usercode", Constants.TIANZIFANG_USERCODE);
            jsonObject.put("password", Constants.TIANZIFANG_PASSWORD);
            StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
            post.setEntity(s);
            HttpResponse execute = httpClient.execute(post);
            String entity = EntityUtils.toString(execute.getEntity(), "utf-8");
            JSONObject jsonObject1 = JSON.parseObject(entity);
//            HpScenicNumber hp = new HpScenicNumber();
            LogFactory.getLog(this.getClass()).info("获取接口数据成功logintianzifang：http://121.40.116.34:8077/User/Login");
            if (jsonObject1.getString("success") == "true") {

                JSONObject data = JSON.parseObject(jsonObject1.getString("data"));
                Constants.map.put("tianzifang_uuid", data.getString("uuid"));
                insertCommons(data);

            }
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).error("获取接口数据失败logintianzifang：http://121.40.116.34:8077/User/Login");
        }
    }
    public void loginwaitan() {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://11.66.236.1:8080/User/Login");
            post.setHeader("Content-Type", "application/json;charset=utf-8");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("usercode", Constants.WAITAN_USERCODE);
            jsonObject.put("password", Constants.WAITAN_PASSWORD);
            StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
            post.setEntity(s);
            HttpResponse execute = httpClient.execute(post);
            String entity = EntityUtils.toString(execute.getEntity(), "utf-8");
            JSONObject jsonObject1 = JSON.parseObject(entity);

            LogFactory.getLog(this.getClass()).info("获取接口数据成功loginwaitan：http://121.40.116.34:8077/User/Login");
            if (jsonObject1.getString("success") == "true") {
                JSONObject data = JSON.parseObject(jsonObject1.getString("data"));
                insertCommons(data);
                //将登陆获取对应账号的uuid写入缓存中
                Constants.map.put("waitan_uuid", data.getString("uuid"));
            }
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).error("获取接口数据失败loginwaitan：http://121.40.116.34:8077/User/Login");
        }

    }

    public void insertCommons(JSONObject data) {
        HpScenicNumber hp = new HpScenicNumber();
        hp.setUuid(data.getString("uuid"));
        hp.setName(data.getString("name"));
        hp.setIsadmin(data.getString("isadmin"));
        JSONArray spot = JSON.parseArray(data.getString("Spot"));
        hp.setSpotcode(JSON.parseObject(spot.get(0).toString()).getString("spotcode"));
        hp.setSpotname(JSON.parseObject(spot.get(0).toString()).getString("spotname"));
        hp.setMaxHeat(JSON.parseObject(spot.get(0).toString()).getString("maxHeat"));
        hp.setMaxtotal(JSON.parseObject(spot.get(0).toString()).getString("maxtotal"));
        hp.setMaxrealtime(JSON.parseObject(spot.get(0).toString()).getString("maxrealtime"));
        hp.setMaxblue(JSON.parseObject(spot.get(0).toString()).getString("maxblue"));
        hp.setMaxyellow(JSON.parseObject(spot.get(0).toString()).getString("maxyellow"));
        hp.setMaxred(JSON.parseObject(spot.get(0).toString()).getString("maxred"));
        hp.setRealmaxblue(JSON.parseObject(spot.get(0).toString()).getString("realmaxblue"));
        hp.setRealmaxyellow(JSON.parseObject(spot.get(0).toString()).getString("realmaxyellow"));
        hp.setRealmaxred(JSON.parseObject(spot.get(0).toString()).getString("realmaxred"));
        hp.setCreateTime(DateUtil.getDateTime());
        String uuid = syncScenicSpotService.findScenicSpot(data.getString("uuid"));
        if (uuid != null) {
            syncScenicSpotService.updateScenicSpot(hp);
        } else {
            syncScenicSpotService.insertScenicSpot(hp);
        }
    }

public void currentInit(){
    //http://121.40.116.34:8077,正式环境http://11.66.236.1:8080/
    HpScenicSpotCurrentqueryTaskyy();
    HpScenicSpotCurrentqueryTaskxtd();
    HpScenicSpotCurrentqueryTasktzf();
    HpScenicSpotCurrentqueryTaskwt();
}
    /**
     * 用于首页中查询当天累计客流人数、实时人数及当天各小时峰值.豫园外圈,外滩，新天地，田子坊
     */
    public void HpScenicSpotCurrentqueryTaskyy() {
        if (Constants.map.get("yuyuan_uuid") == null) {
            String hpScenicUuid = syncScenicSpotService.findCurrentquery(Constants.YUYUAN_SPOTCODE);
            Constants.map.put("yuyuan_uuid", hpScenicUuid);
        }
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://11.66.236.1:8080/Spots/currentquery");
            post.setHeader("Content-Type", "application/json;charset=utf-8");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("useruuid", Constants.map.get("yuyuan_uuid"));
            jsonObject.put("spotcode", Constants.YUYUAN_SPOTCODE);
            jsonObject.put("dateType", DateUtil.getToday23(23, 59, 59));
            StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
            post.setEntity(s);
            HttpResponse execute = httpClient.execute(post);
            String entity = EntityUtils.toString(execute.getEntity(), "utf-8");
            JSONObject jsonObject1 = JSON.parseObject(entity);
            HpScenicRealTime hp = new HpScenicRealTime();
            if (jsonObject1.getString("success") == "true") {
                JSONArray data = JSON.parseArray(jsonObject1.getString("data"));
                hp.setSpotid(JSON.parseObject(data.get(0).toString()).getString("spotid"))
                        .setUpdatetimer(JSON.parseObject(data.get(0).toString()).getString("updatetimer"))
                        .setTotalnumber(JSON.parseObject(data.get(0).toString()).getString("totalnumber"))
                        .setRealtimenumber(JSON.parseObject(data.get(0).toString()).getString("realtimenumber"))
                        .setTime(JSON.parseObject(data.get(0).toString()).getString("time"))
                        .setHour(JSON.parseObject(data.get(0).toString()).getString("hour"))
                        .setCreateTime(DateUtil.getDateTime());
            }
            syncScenicSpotService.insertScenicSpotByYuYuan(hp);
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).error("获取接口数据失败HpScenicSpotCurrentqueryTaskyy：http://121.40.116.34:8077/User/Login");
        }

    }

    public void HpScenicSpotCurrentqueryTaskxtd() {
        if (Constants.map.get("xintiandi_uuid") == null) {
            String hpScenicUuid = syncScenicSpotService.findCurrentquery(Constants.XINTIANDI_SPOTCODE);
            Constants.map.put("xintiandi_uuid", hpScenicUuid);
        }
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://11.66.236.1:8080/Spots/currentquery");
            post.setHeader("Content-Type", "application/json;charset=utf-8");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("useruuid", Constants.map.get("xintiandi_uuid"));
            jsonObject.put("spotcode", Constants.XINTIANDI_SPOTCODE);
            jsonObject.put("dateType", DateUtil.getToday23(23, 59, 59));
            StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
            post.setEntity(s);
            HttpResponse execute = httpClient.execute(post);
            String entity = EntityUtils.toString(execute.getEntity(), "utf-8");
            JSONObject jsonObject1 = JSON.parseObject(entity);
            HpScenicRealTime hp = new HpScenicRealTime();
            if (jsonObject1.getString("success") == "true") {
                JSONArray data = JSON.parseArray(jsonObject1.getString("data"));
                hp.setSpotid(JSON.parseObject(data.get(0).toString()).getString("spotid"))
                        .setUpdatetimer(JSON.parseObject(data.get(0).toString()).getString("updatetimer"))
                        .setTotalnumber(JSON.parseObject(data.get(0).toString()).getString("totalnumber"))
                        .setRealtimenumber(JSON.parseObject(data.get(0).toString()).getString("realtimenumber"))
                        .setTime(JSON.parseObject(data.get(0).toString()).getString("time"))
                        .setHour(JSON.parseObject(data.get(0).toString()).getString("hour"))
                        .setCreateTime(DateUtil.getDateTime());
            }
            syncScenicSpotService.insertScenicSpotByYuYuan(hp);
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).error("获取接口数据失败：http://121.40.116.34:8077/User/Login");

        }
    }

    public void HpScenicSpotCurrentqueryTasktzf() {
        if (Constants.map.get("tianzifang_uuid") == null) {
            String hpScenicUuid = syncScenicSpotService.findCurrentquery(Constants.TIANZIFANG_SPOTCODE);
            Constants.map.put("tianzifang_uuid", hpScenicUuid);
        }
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://11.66.236.1:8080/Spots/currentquery");
            post.setHeader("Content-Type", "application/json;charset=utf-8");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("useruuid", Constants.map.get("tianzifang_uuid"));
            jsonObject.put("spotcode", Constants.TIANZIFANG_SPOTCODE);
            jsonObject.put("dateType", DateUtil.getToday23(23, 59, 59));
            StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
            post.setEntity(s);
            HttpResponse execute = httpClient.execute(post);
            String entity = EntityUtils.toString(execute.getEntity(), "utf-8");
            JSONObject jsonObject1 = JSON.parseObject(entity);
            HpScenicRealTime hp = new HpScenicRealTime();
            if (jsonObject1.getString("success") == "true") {
                JSONArray data = JSON.parseArray(jsonObject1.getString("data"));
                hp.setSpotid(JSON.parseObject(data.get(0).toString()).getString("spotid"))
                        .setUpdatetimer(JSON.parseObject(data.get(0).toString()).getString("updatetimer"))
                        .setTotalnumber(JSON.parseObject(data.get(0).toString()).getString("totalnumber"))
                        .setRealtimenumber(JSON.parseObject(data.get(0).toString()).getString("realtimenumber"))
                        .setTime(JSON.parseObject(data.get(0).toString()).getString("time"))
                        .setHour(JSON.parseObject(data.get(0).toString()).getString("hour"))
                        .setCreateTime(DateUtil.getDateTime());
            }
            syncScenicSpotService.insertScenicSpotByYuYuan(hp);
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).error("获取接口数据失败：http://121.40.116.34:8077/User/Login");

        }
    }

    public void HpScenicSpotCurrentqueryTaskwt() {
        if (Constants.map.get("waitan_uuid") == null) {
            String hpScenicUuid = syncScenicSpotService.findCurrentquery(Constants.WAITAN_SPOTCODE);
            Constants.map.put("waitan_uuid", hpScenicUuid);
        }
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://11.66.236.1:8080/Spots/currentquery");
            post.setHeader("Content-Type", "application/json;charset=utf-8");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("useruuid", Constants.map.get("waitan_uuid"));
            jsonObject.put("spotcode", Constants.WAITAN_SPOTCODE);
            jsonObject.put("dateType", DateUtil.getToday23(23, 59, 59));
            StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
            post.setEntity(s);
            HttpResponse execute = httpClient.execute(post);
            String entity = EntityUtils.toString(execute.getEntity(), "utf-8");
            JSONObject jsonObject1 = JSON.parseObject(entity);
            HpScenicRealTime hp = new HpScenicRealTime();
            if (jsonObject1.getString("success") == "true") {
                JSONArray data = JSON.parseArray(jsonObject1.getString("data"));
                hp.setSpotid(JSON.parseObject(data.get(0).toString()).getString("spotid"))
                        .setUpdatetimer(JSON.parseObject(data.get(0).toString()).getString("updatetimer"))
                        .setTotalnumber(JSON.parseObject(data.get(0).toString()).getString("totalnumber"))
                        .setRealtimenumber(JSON.parseObject(data.get(0).toString()).getString("realtimenumber"))
                        .setTime(JSON.parseObject(data.get(0).toString()).getString("time"))
                        .setHour(JSON.parseObject(data.get(0).toString()).getString("hour"))
                        .setCreateTime(DateUtil.getDateTime());
            }
            syncScenicSpotService.insertScenicSpotByYuYuan(hp);
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).error("获取接口数据失败HpScenicSpotCurrentqueryTaskwt：http://121.40.116.34:8077/User/Login");

        }
    }
}
